package com.mouxidea;

import java.util.Map;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;

public class Config {
	
	private static Config instance = null;

	private static final String CONFIG_FILE_NAME = "config";
	private static final String DB_IP = "DB_IP";
	private static final String DB_PORT = "DB_PORT";
	private static final String DB_USERNAME = "DB_USERNAME";
	private static final String DB_PASSWORD = "DB_PASSWORD";
	private static final String DB_NAME = "DB_NAME";
	private static final String OUTPUT_FOLDER = "OUTPUT_FOLDER";
	private static final String SQL_ID = "SQL_ID";
	private static final String SQL_TODAY = "SQL_TODAY";
	private static final String SQL_1WK = "SQL_1WK";
	private static final String SQL_1MTH = "SQL_1MTH";
	private static final String COMPARISON = "COMPARISON";
	private static final String THRESHOLD = "THRESHOLD";
	private static final String MAIL_HOST = "MAIL_HOST";
	private static final String MAIL_PORT = "MAIL_PORT";
	private static final String MAIL_FROM = "MAIL_FROM";
	private static final String MAIL_PASSWORD = "MAIL_PASSWORD";
	private static final String MAIL_TO = "MAIL_TO";
	private static final String MAIL_SUBJECT = "MAIL_SUBJECT";
	private static final String MAIL_SECURITY_LEVEL = "MAIL_SECURITY_LEVEL";
	private static final String SNMP_OID = "SNMP_OID";
	private static final String SNMP_IP = "SNMP_IP";
	private static final String SNMP_PORT = "SNMP_PORT";
	private static final String SNMP_VERSION = "SNMP_VERSION";
	private static final String SNMP_COMMUNITY = "SNMP_COMMUNITY";
	private static final String SQL_CUSTOMIZATION = "SQL_CUSTOMIZATION";
	private static final String UTILIZATION = "UTILIZATION";
	
	
	public static final String MAX_UTIL_H = "MAX_UTIL_H";
	public static final String MAX_IN_UTIL_F = "MAX_IN_UTIL_F";
	public static final String MAX_OUT_UTIL_F = "MAX_OUT_UTIL_F";
	public static final String AVG_UTIL_H = "AVG_UTIL_H";
	public static final String AVG_IN_UTIL_F = "AVG_IN_UTIL_F";
	public static final String AVG_OUT_UTIL_F = "AVG_OUT_UTIL_F";
	public static final String SECURITY_LEVEL_HIGH = "H";
	public static final String SECURITY_LEVEL_LOW = "L";
	public static final String SNMP_V1 = "1";
	public static final String SNMP_V2 = "2";
	
	private String dbIp = null;
	private String dbPort = null;
	private String dbUsername = null;
	private String dbPassword = null;
	private String dbName = null;
	private String outputFolderName = null;
	private String sqlId = null;
	private String sqlToday = null;
	private String sql1Wk = null;
	private String sql1Mth = null;
	private String comparisonStr = null;
	private double threshold = 5;//5%
	private String thresholdStr = null;
	private String mailHost = null;
	private String mailPort = null;
	private String mailFrom = null;
	private String mailPassword = null;
	private String mailTo = null;
	private String mailSubject = null;
	private String mailSecurityLevel = null;
	private String snmpOid = null;
	private String snmpIp = null;
	private String snmpPort = null;
	private String snmpVersion = null;
	private String snmpCommunity = null;
	private String utilizationStr = null;
	private String sqlCustomization = null;
	private Map<String, ComparisonRule> comparisonMap = new ConcurrentHashMap<String, ComparisonRule>();
	private Map<String, UtilizationRule> utilizationMap = new ConcurrentHashMap<String, UtilizationRule>();
	
	private Config(){
		ResourceBundle configResource = PropertyResourceBundle.getBundle(CONFIG_FILE_NAME);
		
		this.dbIp = configResource.getString(DB_IP);
		this.dbPort = configResource.getString(DB_PORT);
		this.dbUsername = configResource.getString(DB_USERNAME);
		this.dbPassword = configResource.getString(DB_PASSWORD);
		this.dbName = configResource.getString(DB_NAME);
		this.outputFolderName = configResource.getString(OUTPUT_FOLDER);
		this.sqlId = configResource.getString(SQL_ID);
		this.sqlToday = configResource.getString(SQL_TODAY);
		this.sql1Wk = configResource.getString(SQL_1WK);
		this.sql1Mth = configResource.getString(SQL_1MTH);
		this.comparisonStr = configResource.getString(COMPARISON);
		this.thresholdStr = configResource.getString(THRESHOLD);
		this.mailHost = configResource.getString(MAIL_HOST);
		this.mailPort = configResource.getString(MAIL_PORT);
		this.mailFrom = configResource.getString(MAIL_FROM);
		this.mailPassword = configResource.getString(MAIL_PASSWORD);
		this.mailTo = configResource.getString(MAIL_TO);
		this.mailSubject = configResource.getString(MAIL_SUBJECT);
		this.mailSecurityLevel = configResource.getString(MAIL_SECURITY_LEVEL);
		this.snmpOid = configResource.getString(SNMP_OID);
		this.snmpIp = configResource.getString(SNMP_IP);
		this.snmpPort = configResource.getString(SNMP_PORT);
		this.snmpVersion = configResource.getString(SNMP_VERSION);
		this.snmpCommunity = configResource.getString(SNMP_COMMUNITY);
		this.utilizationStr = configResource.getString(UTILIZATION);
		this.sqlCustomization = configResource.getString(SQL_CUSTOMIZATION);
		
		
		if (this.thresholdStr!=null && !"".equals(this.thresholdStr.trim()))
		{
			this.threshold = Double.parseDouble(thresholdStr);
		}
		
		if (this.comparisonStr!=null && !"".equals(this.comparisonStr.trim())){
			String[] comparisonPairs = this.comparisonStr.split(";");
			for (String pair : comparisonPairs){
				if (pair!=null && !"".equals(pair.trim())){
					String[] tokens = pair.split(":");
					if (tokens!=null && tokens.length==2){
						this.comparisonMap.put(tokens[0], new ComparisonRule(tokens[0], tokens[1], threshold));
					}
					else if (tokens!=null && tokens.length>2){
						this.comparisonMap.put(tokens[0], new ComparisonRule(tokens[0], tokens[1], Double.parseDouble(tokens[2])));
					}
				}
			}
		}
		
		if (this.utilizationStr!=null && !"".equals(this.utilizationStr.trim())){
			String[] ultilizationInfoArray = this.utilizationStr.split(";");
			for (String ultilizationInfo : ultilizationInfoArray){
				if (ultilizationInfo!=null && !"".equals(ultilizationInfo.trim())){
					String[] tokens = ultilizationInfo.split(":");
					if (tokens!=null && tokens.length>=6){
						this.utilizationMap.put(tokens[0], new UtilizationRule(tokens[0], tokens[1], Double.parseDouble(tokens[2]), Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4]), Integer.parseInt(tokens[5])));
					}
				}
			}
		}
	}
	
	public static Config getInstance(){
		if (instance == null){
			instance = new Config();
		}
		
		return instance;
	}
	
	public String getDbIp() {
		return dbIp;
	}
	public void setDbIp(String dbIp) {
		this.dbIp = dbIp;
	}
	public String getDbPort() {
		return dbPort;
	}
	public void setDbPort(String dbPort) {
		this.dbPort = dbPort;
	}
	public String getDbUsername() {
		return dbUsername;
	}
	public void setDbUsername(String dbUsername) {
		this.dbUsername = dbUsername;
	}
	public String getDbPassword() {
		return dbPassword;
	}
	public void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;
	}
	public String getDbName() {
		return dbName;
	}
	public void setDbName(String dbName) {
		this.dbName = dbName;
	}
	public String getOutputFolderName() {
		return outputFolderName;
	}
	public void setOutputFolderName(String outputFolderName) {
		this.outputFolderName = outputFolderName;
	}
	
	public String getSqlId() {
		return sqlId;
	}

	public void setSqlId(String sqlId) {
		this.sqlId = sqlId;
	}

	public String getSqlToday() {
		return sqlToday;
	}

	public void setSqlToday(String sqlToday) {
		this.sqlToday = sqlToday;
	}

	public String getSql1Wk() {
		return sql1Wk;
	}

	public void setSql1Wk(String sql1Wk) {
		this.sql1Wk = sql1Wk;
	}

	public String getSql1Mth() {
		return sql1Mth;
	}

	public void setSql1Mth(String sql1Mth) {
		this.sql1Mth = sql1Mth;
	}

	public Map<String, ComparisonRule> getComparisonMap() {
		return comparisonMap;
	}
	
	public double getThreshold() {
		return threshold;
	}

	public void setComparisonMap(Map<String, ComparisonRule> comparisonMap) {
		this.comparisonMap = comparisonMap;
	}

	public String getMailHost() {
		return mailHost;
	}

	public void setMailHost(String mailHost) {
		this.mailHost = mailHost;
	}

	public String getMailPort() {
		return mailPort;
	}

	public void setMailPort(String mailPort) {
		this.mailPort = mailPort;
	}

	public String getMailFrom() {
		return mailFrom;
	}

	public void setMailFrom(String mailFrom) {
		this.mailFrom = mailFrom;
	}

	public String getMailPassword() {
		return mailPassword;
	}

	public void setMailPassword(String mailPassword) {
		this.mailPassword = mailPassword;
	}

	public String getMailTo() {
		return mailTo;
	}

	public void setMailTo(String mailTo) {
		this.mailTo = mailTo;
	}

	public String getMailSubject() {
		return mailSubject;
	}

	public void setMailSubject(String mailSubject) {
		this.mailSubject = mailSubject;
	}

	public String getMailSecurityLevel() {
		return mailSecurityLevel;
	}

	public void setMailSecurityLevel(String mailSecurityLevel) {
		this.mailSecurityLevel = mailSecurityLevel;
	}

	public String getSnmpOid() {
		return snmpOid;
	}

	public void setSnmpOid(String snmpOid) {
		this.snmpOid = snmpOid;
	}

	public String getSnmpIp() {
		return snmpIp;
	}

	public void setSnmpIp(String snmpIp) {
		this.snmpIp = snmpIp;
	}

	public String getSnmpPort() {
		return snmpPort;
	}

	public void setSnmpPort(String snmpPort) {
		this.snmpPort = snmpPort;
	}

	public String getSnmpVersion() {
		return snmpVersion;
	}

	public void setSnmpVersion(String snmpVersion) {
		this.snmpVersion = snmpVersion;
	}

	public Map<String, UtilizationRule> getUtilizationMap() {
		return utilizationMap;
	}

	public void setUtilizationMap(Map<String, UtilizationRule> utilizationMap) {
		this.utilizationMap = utilizationMap;
	}

	public String getSqlCustomization() {
		return sqlCustomization;
	}

	public void setSqlCustomization(String sqlCustomization) {
		this.sqlCustomization = sqlCustomization;
	}

	public String getSnmpCommunity() {
		return snmpCommunity;
	}

	public void setSnmpCommunity(String snmpCommunity) {
		this.snmpCommunity = snmpCommunity;
	}

	public static void main(String[] args){
		String comparisonStr = "id1:MAX_UTIL_H;id2:MAX_IN_UTIL_F;id3:MAX_OUT_UTIL_F";
		if (comparisonStr!=null && !"".equals(comparisonStr.trim())){
			String[] comparisonPairs = comparisonStr.split(";");
			for (String pair : comparisonPairs){
				if (pair!=null && !"".equals(pair.trim())){
					String[] tokens = pair.split(":");
					if (tokens!=null && tokens.length>=2){
						System.out.println("key="+tokens[0]);
						System.out.println("value="+tokens[1]);
					}
				}
			}
		}
	}
	
}
